<?php
namespace Info\Controller;

use Common\Controller\BehaviorController;


class WeatherController extends BehaviorController
{

    public function index(){
        $this->display('/weather/index');
    }

//    public function index()
//    {
//        $url = "http://weather.ssl.qhimg.com/sed_api_weather_info.php?code=101201101&_jsonp=getWeather";
//        $weather = curl($url);
//        $weather = str_replace(array("getWeather(", ");"), "", $weather);
//        $weather = json_decode($weather, true);
//
//        $this->assign('weather', $weather);
//
//        $this->display(":weather_shiyan");
//    }

    public function top(){
        $this->display(":weather_test");
    }

    public function test(){
        $str = sprintf("ts=%s&ttl=30&uid=U4CB4A5224",time());
        $key = "afmx8hku9ubflkfn";
        $sig = $this->getSignature($str,$key);

        $url = sprintf("https://api.thinkpage.cn/v3/weather/now.json?location=beijing&%s&sig=%s&callback=showWeather",$str,$sig);

        $url1 = 'https://api.thinkpage.cn/v3/life/suggestion.json?key=afmx8hku9ubflkfn&location=shanghai&language=zh-Hans';

        $url2 = "https://api.heweather.com/x3/weather?cityid=CN101010100&key=086def76c04542d7895d188603714494";

        $res = curl($url2);

        echo ($res);
    }

    public function getSignature($str, $key) {
        $signature = "";
        if (function_exists('hash_hmac')) {
            $signature = base64_encode(hash_hmac("sha1", $str, $key, true));
        } else {
            $blocksize = 64;
            $hashfunc = 'sha1';
            if (strlen($key) > $blocksize) {
                $key = pack('H*', $hashfunc($key));
            }
            $key = str_pad($key, $blocksize, chr(0x00));
            $ipad = str_repeat(chr(0x36), $blocksize);
            $opad = str_repeat(chr(0x5c), $blocksize);
            $hmac = pack(
                'H*', $hashfunc(
                    ($key ^ $opad) . pack(
                        'H*', $hashfunc(
                            ($key ^ $ipad) . $str
                        )
                    )
                )
            );
            $signature = base64_encode($hmac);
        }
        return $signature;
    }

}